package com.chanjiedata.changbs.module.main;

/**
 * 文件名 : AllocationDetailActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/27 14:25
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Mystock;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.chanjiedata.changbs.utlis.UIUtils;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AllocationDetailActivity extends BaseActivity {


    @BindView(R.id.tv_my_name)
    TextView tvMyName;
    @BindView(R.id.tv_your_name)
    TextView tvYourName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_jiju_name)
    TextView tvJijuName;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_take_money)
    Button tvTakeMoney;
    @BindView(R.id.sn_list)
    ListView snList;
    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.tv_title)
    TextView tvTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allocation_detail);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        tvTitle.setText("调拨详情");
        initListener();
        initData();
    }

    private void initData() {
        HashMap<String, String> params = new HashMap<>();
        Mystock.allocationLogInfo.Builder builder = Mystock.allocationLogInfo.newBuilder();
        builder.setId(getIntent().getStringExtra("id"));
        NetworkManager.INSTANCE.post(Apis.allocationLogInfo, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                Mystock.allocationLogInfo data = Mystock.allocationLogInfo.parseFrom(bytes);
                setViewData(data);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {

            }
        });
    }


    private void initListener() {
        backTv.setOnClickListener(view -> {
            finish();
        });
        tvTakeMoney.setOnClickListener(view -> {
            ConfirmMoneyTwo();
        });
    }


    private void setViewData(Mystock.allocationLogInfo data) {
        tvMyName.setText(data.getMemberInitiator());
        tvYourName.setText(data.getRecipientName());
        tvPhone.setText(data.getRecipientMobile());
        tvJijuName.setText(data.getEquipment());
        tvNumber.setText(data.getEquipmentNum() + "");
        tvTime.setText(data.getEquipmentTime());
        if (data.getIsType().equals("0")) {
            tvTakeMoney.setText("未收款");
        } else {
            tvTakeMoney.setText("已收款");
        }
        if (data.getIsConfirm().equals("1")) {
            tvTakeMoney.setText("确认收款");
            tvTakeMoney.setEnabled(true);
        } else {
            tvTakeMoney.setEnabled(false);
        }
        tvTakeMoney.setVisibility(View.VISIBLE);
        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.item_allow_layout
                , data.getSnCodeList().split(","));
        snList.setAdapter(adapter);
        setListViewHeight(snList);

    }


    private void ConfirmMoneyTwo() {
        showProgress();
        Mystock.allocationLogInfo.Builder builder = Mystock.allocationLogInfo.newBuilder();
        builder.setId(getIntent().getStringExtra("id"));

        NetworkManager.INSTANCE.post(Apis.allocationConfirm, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                hideProgress();
                if (errorCode == 200) {
                    tvTakeMoney.setText("已收款");
                    tvTakeMoney.setEnabled(false);
                    setResult(200);
                }
                showToast(errorMessage);
            }
        });

    }


    private void setListViewHeight(ListView listView) {
        Adapter listAdapter = listView.getAdapter();   //得到ListView 添加的适配器
        View itemView = listAdapter.getView(0, null, listView); //获取其中的一项
        //进行这一项的测量，为什么加这一步，具体分析可以参考 https://www.jianshu.com/p/dbd6afb2c890这篇文章
        itemView.measure(0, 0);
        int itemHeight = itemView.getMeasuredHeight(); //一项的高度
        int itemCount = listAdapter.getCount();//得到总的项数
        LinearLayout.LayoutParams layoutParams = null; //进行布局参数的设置
        if (itemCount <= 5) {
            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dip2Px(this, 40) * itemCount);
        } else if (itemCount > 5) {
            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dip2Px(this, 40) * 5);
        }
        listView.setLayoutParams(layoutParams);
    }


}

